<?php

/**
 * Creates childs for the Phlame-Hierarchy
 * Parent's Factory uses the setOwner interface
 * OwnedFactory is a DecoraterFactory, it can be nested with other Factories
 */
class OwnedFactory extends AbstractDecoratorFactory implements setOwner
{
    protected $_ownedType;
    /**
     * @var Phlame
     */
    protected $_Owner = NULL;
    
    public function __construct(Factory $Factory, $ownedType)
    {
        parent::__construct($Factory);
        if (empty($ownedType) || !is_string($ownedType)) {
            throw new InvalidArgumentException('ownedType empty or not string');
        }
        $this->_ownedType = $ownedType;
    }
    
    public function setOwner(Phlame $Owner = NULL)
    {
        $this->_Owner = $Owner;
    }
    
    public function create(array $data)
    {
        if (is_null($this->_Owner)) {
            throw new FactoryException('Owner not set');
        }
        if ($data[constant(get_class($this->_Owner).'::'.$this->_ownedType.'_BINDER')] != $this->_Owner->getID()) {
            return false;
        }
        $Phlame = parent::create($data);
        $Phlame->setOwner($this->_Owner);
        return $Phlame;
    }
}